Option Strict Off
Option Explicit On
Module ExitandCleanup
	'This module can be called from various part of the program, it cleans up memory and exits the program back to windows.
	
	Public Sub ExitProgram() 'Exit Button has been pressed
		If ErrorCodeNo <> "00.00" Then GoTo ExitWithError : 'There has been an error and the program is trying to exit gracefully
		
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live mode before asking to exit
		
		If MsgBox("Do you want to exit Helium Frog Animator?" & Chr(13) & Chr(10) & "Animation is automatically saved as" & Chr(13) & Chr(10) & ProjName & ".avi", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.Yes Then
			
			Call WriteSettingData() 'Writes all the Program settings (Window sizes etc.) to the User_settings.txt file
ExitWithError: 
			mblnRunning = False 'Stop the main loop
			
			
			Call ReleaseKeyboard() 'Remove the keyboard hook
			
			Call frmRotoscope.RemoveDShowRotoscope() 'Clean up the rotoscope mediaplayer etc.
			Call RemoveDShowPlayBack() 'Clean up the playback mediaplayer etc.
			Call RemoveDShowEditor() 'Clean up Editor media player and variables if they are active
			Call AudioPlayer_Remove() 'Clean up the soundtrack audio player
			
			CleanDC(Hotspot_Source)
			CleanDC(Hotspot_Buttons)
			CleanDC(Hotspot_Settings)
			frmRotoscope.Close()
			frmAveraging.Close()
			frmEditor.Close()
			frmCapture.Close() 'unload the forms. Must be done as Aspect ratio module when used will crash program
			frmAnimator.Close()
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\LastFrame.jpg") <> "" Then Kill((My.Application.Info.DirectoryPath & "\LastFrame.jpg")) 'Erase the temporary lastframe captured store if present
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\Undo.jpg") <> "" Then Kill((My.Application.Info.DirectoryPath & "\Undo.jpg")) 'Erase the temporary Undo frame if present
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\RapidBuild.avi") <> "" Then Kill((My.Application.Info.DirectoryPath & "\RapidBuild.avi")) 'Erase the temporary Undo frame if present
			End
		Else
			ExitHotkeyPressed = False 'The exit may have been called with a hotkey, so reset the call flag so it doesnt loop again
		End If
		If ErrorLoggingYesNO = True Then PrintLine(181, "Frame captured successfully")
		If ErrorLoggingYesNO = True Then FileClose(181) 'Close the errorlog file
	End Sub
End Module